home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / jDXEngine 23886812001.psc / clsObject.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-08-01  |  12.6 KB  |  395 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsUnlitObject"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'This object's purpose is to group specific geometry together as a single entity
  17.  
  18. 'The texture to be used when drawn
  19. Dim Texture As Direct3DTexture8
  20.  
  21. 'The primitive type to be used when drawn
  22. Dim mPrimitiveType As PRIMITIVES
  23.  
  24. 'The orientation of this geometry-set
  25. 'Please note that this is not the 'World Space' matrix itself, but
  26. 'a 'personal' World Space for this particular geometry-set
  27. Dim matWorld As D3DMATRIX
  28.  
  29. 'Unlit Vertex Type used by DirectX (Same as D3DVERTEX)
  30. Private Type VERTEX
  31.     X As Single
  32.     Y As Single
  33.     z As Single
  34.     nx As Single
  35.     ny As Single
  36.     nz As Single
  37.     tu As Single
  38.     tv As Single
  39. End Type
  40.  
  41. 'Array of vertices to use when drawing
  42. Private Vertices() As VERTEX
  43.  
  44. 'Vertex type definition for DirectX
  45. Private Const FVF_UNLITVERTEX = (D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1)
  46.  
  47. 'Types of Primitives
  48. Public Enum PRIMITIVES
  49.     TriangleList = D3DPT_TRIANGLELIST
  50.     TriangleFan = D3DPT_TRIANGLEFAN
  51.     TraingleStrip = D3DPT_TRIANGLESTRIP
  52. End Enum
  53.  
  54. 'This sub will add a cube given the left-top-back corner, and the right-bottom-front corners
  55. 'x1,y1,z1,x2,y2,z2:     The two corners of the cube
  56. 'z3,z4:                 The Z coordinates of the left-bottom-front corner and right-top-back corner to determine orientation
  57. Public Sub AddCube(x1 As Single, y1 As Single, z1 As Single, x2 As Single, y2 As Single, z2 As Single, z3 As Single, z4 As Single)
  58.     On Error GoTo error_h
  59.     
  60.     'Just make the faces with the appropriate values
  61.     AddRect x1, y1, z2, x2, y2, z2, z3, z3  'Side 1
  62.     AddRect x1, y1, z1, x2, y1, z2, z3, z4  'Side 2
  63.     AddRect x1, y2, z1, x2, y1, z1, z1, z4  'Side 3
  64.     AddRect x1, y2, z2, x2, y2, z1, z4, z2  'Side 4
  65.     AddRect x1, y1, z1, x1, y2, z2, z4, z3  'Side 5
  66.     AddRect x2, y1, z2, x2, y2, z1, z2, z4  'Side 6
  67.  
  68.     Exit Sub
  69. error_h:
  70.     Select Case ErrMsg(Err, "clsUnlitObject.AddCube")
  71.         Case vbRetry
  72.             Resume
  73.         Case vbIgnore
  74.             Resume Next
  75.         Case Else
  76.             Exit Sub
  77.     End Select
  78. End Sub
  79.  
  80. 'This will create a 2D rectangle out of 2 triangles given the top-left and bottom-right coordinates
  81. 'x1,y1,z1,x2,y2,z2:     top-left, and bottom-right coords of the rectangle
  82. 'z3,z4:                 Z coordinates of bottom-left and top-right corners for orientation
  83. '[strTexture]:          Unused at this time.  Was originally meant for a texture, but now is obsolete.
  84. '                       (This argument WILL be removed in the next version)
  85. Public Sub AddRect(x1 As Single, y1 As Single, z1 As Single, x2 As Single, y2 As Single, z2 As Single, z3 As Single, z4 As Single, Optional strTexture As String)
  86.     On Error GoTo error_h
  87.     
  88.     If IsMissing(strTexture) Then
  89.         AddTriangle x1, y1, z1, x2, y2, z2, x1, y2, z3, 0
  90.         AddTriangle x1, y1, z1, x2, y1, z4, x2, y2, z2, 0
  91.     Else
  92.         AddTriangle x1, y1, z1, x2, y2, z2, x1, y2, z3, 0, strTexture
  93.         AddTriangle x1, y1, z1, x2, y1, z4, x2, y2, z2, 1, strTexture
  94.     End If
  95.     
  96.     Exit Sub
  97. error_h:
  98.     Select Case ErrMsg(Err, "clsUnlitObject.AddRect")
  99.         Case vbRetry
  100.             Resume
  101.         Case vbIgnore
  102.             Resume Next
  103.         Case Else
  104.             Exit Sub
  105.     End Select
  106. End Sub
  107.  
  108. 'This will create a 2D triangle based off 3 points
  109. 'x(1-3),y(1-3),z(1-3):  xyz coordinates of each point in the triangle
  110. 'tValue:                1 if this triangle is the top-half of a rectangle. Used for texture mapping
  111. '[strTexture]:          Unused.  Will be removed in next update
  112. Public Sub AddTriangle(x1 As Single, y1 As Single, z1 As Single, x2 As Single, y2 As Single, z2 As Single, x3 As Single, y3 As Single, z3 As Single, tValue As Integer, Optional strTexture As String)
  113.     On Error GoTo error_h
  114.     
  115.     'Vertices to be created
  116.     Dim v1 As VERTEX, v2 As VERTEX, v3 As VERTEX
  117.     'A 'normal' is a vector that describes which way the vertices are facing
  118.     Dim Normal As D3DVECTOR
  119.     
  120.     If IsMissing(strTexture) Then       'Untextured triangle (Not gonna happen with unlit objects)
  121.         'v1 = MakeVertex(x1, y1, z1, 0, 0, 0, 0, 0)
  122.         'v2 = MakeVertex(x2, y2, z2, 0, 0, 0, 0, 0)
  123.         'v3 = MakeVertex(x3, y3, z3, 0, 0, 0, 0, 0)
  124.     ElseIf tValue = 1 Then
  125.         'Top half of the rect (if this is even PART of a rect...)
  126.         v1 = MakeVertex(x1, y1, z1, 0, 0, 0, 0, 0)
  127.         v2 = MakeVertex(x2, y2, z2, 0, 0, 0, 1, 0)
  128.         v3 = MakeVertex(x3, y3, z3, 0, 0, 0, 1, 1)
  129.     Else
  130.         'Bottom half of the theoretical rect...
  131.         v1 = MakeVertex(x1, y1, z1, 0, 0, 0, 0, 0)
  132.         v2 = MakeVertex(x2, y2, z2, 0, 0, 0, 1, 1)
  133.         v3 = MakeVertex(x3, y3, z3, 0, 0, 0, 0, 1)
  134.     End If
  135.     
  136.     'This will get the direction that the triangle is facing ASSUMING that the vertices are
  137.     'in clockwise order from v1 to v2 to v3.  If they are in counter-clockwise order, the
  138.     'normal will point in the opposite direction
  139.     Normal = GetTriangleNormal(v1, v2, v3)
  140.     
  141.     'Copy the normal values to all the created vertices
  142.     With Normal
  143.         v1.nx = .X
  144.         v1.ny = .Y
  145.         v1.nz = .z
  146.         v2.nx = .X
  147.         v2.ny = .Y
  148.         v2.nz = .z
  149.         v3.nx = .X
  150.         v3.ny = .Y
  151.         v3.nz = .z
  152.     End With
  153.     
  154.     'Add these vertices to the array
  155.     mAddVertex v1
  156.     mAddVertex v2
  157.     mAddVertex v3
  158.     
  159.     Exit Sub
  160. error_h:
  161.     Select Case ErrMsg(Err, "clsUnlitObject.AddTriangle")
  162.         Case vbRetry
  163.             Resume
  164.         Case vbIgnore
  165.             Resume Next
  166.         Case Else
  167.             Exit Sub
  168.     End Select
  169. End Sub
  170. 'This will add a vertex object to the containing array to be drawn
  171. 'x,y,z:     World Space coordinates of this vertex
  172. 'nx,ny,nz:  World Space coordinates of which way this vertex is pointing
  173. 'tu,tv:     Texture map coordinates of this vertex
  174. 'Please note that this sub is meant to add custom shapes to your app, and is NOT used when adding
  175. 'cubes/rects to World Space.  It can also be used for custom texture-mapping.
  176. Public Sub AddVertex(X As Single, Y As Single, z As Single, nx As Single, ny As Single, nz As Single, tu As Single, tv As Single)
  177.     On Error GoTo error_h
  178.     
  179.     'Create a vertex
  180.     Dim v As VERTEX
  181.     
  182.     'Assign values
  183.     With v
  184.         .X = X
  185.         .Y = Y
  186.         .z = z
  187.         .nx = nx
  188.         .ny = ny
  189.         .nz = nz
  190.         .tu = tu
  191.         .tv = tv
  192.     End With
  193.     
  194.     'Add the vertex
  195.     mAddVertex v
  196.     
  197.     Exit Sub
  198. error_h:
  199.     Select Case ErrMsg(Err, "clsUnlitObject.AddVertex")
  200.         Case vbRetry
  201.             Resume
  202.         Case vbIgnore
  203.             Resume Next
  204.         Case Else
  205.             Exit Sub
  206.     End Select
  207. End Sub
  208.  
  209.  
  210. 'This sub's purpose is to take a created vertex, and store it in the Vertices() array
  211. 'v: The VERTEX object to store
  212. Private Sub mAddVertex(v As VERTEX)
  213.     On Error GoTo error_h
  214.     
  215.     On Error Resume Next
  216.     ReDim Preserve Vertices(UBound(Vertices) + 1)   'Increase the array by one
  217.     If Err Then     'There were no vertices in the array, so make the first
  218.         Err.Clear
  219.         ReDim Vertices(0)
  220.     End If
  221.     Vertices(UBound(Vertices)) = v  'Set the vertex passed to this sub as the last vertex in the list
  222.     
  223.     Exit Sub
  224. error_h:
  225.     Select Case ErrMsg(Err, "clsUnlitObject.AddVertex")
  226.         Case vbRetry
  227.             Resume
  228.         Case vbIgnore
  229.             Resume Next
  230.         Case Else
  231.             Exit Sub
  232.     End Select
  233. End Sub
  234.  
  235. 'This sub will actually perform the drawing of all the geometry for this object
  236. 'D3DDevice:     Passed from jDXEngine, this is the device to draw upon
  237. Public Sub Draw(D3DDevice As Direct3DDevice8)
  238.     On Error GoTo error_h
  239.     
  240.     'The count of primitives (triangles)
  241.     Dim intCount As Integer
  242.     
  243.     'Use the proper texture for this object.
  244.     'If no texture exists, use default white
  245.     If Not Texture Is Nothing Then
  246.         D3DDevice.SetTexture 0, Texture
  247.     Else
  248.         D3DDevice.SetTexture 0, Nothing
  249.     End If
  250.     
  251.     'Set the world matrix to this object's own matrix so that it can be rotated independantly of the others
  252.     D3DDevice.SetTransform D3DTS_WORLD, matWorld
  253.     
  254.     'Calculate the number of primitives
  255.     Select Case mPrimitiveType
  256.         Case D3DPT_TRIANGLELIST
  257.             intCount = (UBound(Vertices) + 1) / 3
  258.         Case D3DPT_TRIANGLEFAN
  259.             intCount = UBound(Vertices) - 1
  260.         Case D3DPT_TRIANGLESTRIP
  261.             intCount = UBound(Vertices) + 1 - 2
  262.         Case Else
  263.             Stop
  264.             'Need to figure out primitive count for this mPrimitiveType
  265.     End Select
  266.     
  267.     'Perform the draw operation
  268.     D3DDevice.DrawPrimitiveUP mPrimitiveType, intCount, Vertices(0), Len(Vertices(0))
  269.             
  270.     Exit Sub
  271. error_h:
  272.     Select Case ErrMsg(Err, "clsUnlitObject.Draw")
  273.         Case vbRetry
  274.             Resume
  275.         Case vbIgnore
  276.             Resume Next
  277.         Case Else
  278.             Exit Sub
  279.     End Select
  280. End Sub
  281.  
  282. 'This function will compute the 'normal' of a triangle given the 3 vertices.
  283. 'Please note that the vertices are assumed to be in clockwise order.
  284. 'If they are NOT clockwise, then the vector returned will point in the opposite direction
  285. Private Function GetTriangleNormal(v1 As VERTEX, v2 As VERTEX, v3 As VERTEX) As D3DVECTOR
  286.     On Error GoTo error_h
  287.     
  288.     'Variables used to calculate the normal
  289.     Dim v01 As D3DVECTOR
  290.     Dim v02 As D3DVECTOR
  291.     Dim Normal As D3DVECTOR
  292.     
  293.     'I'm not going to pretend that I understand the mathmatics behind this but , again,
  294.     'this has proven effective from the tutorials that I've read. :)
  295.     
  296.     D3DXVec3Subtract v01, MakeVector(v2.X, v2.Y, v2.z), MakeVector(v1.X, v1.Y, v1.z)
  297.     D3DXVec3Subtract v02, MakeVector(v3.X, v3.Y, v3.z), MakeVector(v1.X, v1.Y, v1.z)
  298.     
  299.     D3DXVec3Cross Normal, v01, v02
  300.     
  301.     D3DXVec3Normalize Normal, Normal
  302.     
  303.     GetTriangleNormal = Normal
  304.     
  305.     Exit Function
  306. error_h:
  307.     Select Case ErrMsg(Err, "clsUnlitObjec.GetTriangleNormal")
  308.         Case vbRetry
  309.             Resume
  310.         Case vbIgnore
  311.             Resume Next
  312.         Case Else
  313.             Exit Function
  314.     End Select
  315. End Function
  316.  
  317. 'A simple function to create a vector from coordinate data
  318. Private Function MakeVector(X As Single, Y As Single, z As Single) As D3DVECTOR
  319.     With MakeVector
  320.         .X = X
  321.         .Y = Y
  322.         .z = z
  323.     End With
  324. End Function
  325.  
  326.  
  327. 'Another simple function to create a VERTEX object by copying the values
  328. 'Please note that the VERTEX type is EXACTLY the same as D3DVERTEX
  329. '(I had already implemented the VERTEX type before I knew that, and didn't feel like re-coding)
  330. Private Function MakeVertex(X As Single, Y As Single, z As Single, nx As Single, ny As Single, nz As Single, tu As Single, tv As Single) As VERTEX
  331.     With MakeVertex
  332.         .X = X
  333.         .Y = Y
  334.         .z = z
  335.         .nx = nx
  336.         .ny = ny
  337.         .nz = nz
  338.         .tu = tu
  339.         .tv = tv
  340.     End With
  341. End Function
  342.  
  343. Property Get PrimitiveType() As PRIMITIVES
  344.     PrimitiveType = mPrimitiveType
  345. End Property
  346.  
  347. Property Let PrimitiveType(lngNewType As PRIMITIVES)
  348.     mPrimitiveType = lngNewType
  349. End Property
  350. 'This sub will rotate the entire object as a single entity
  351. Public Sub RotateY(sngAngle As Single)
  352.     On Error GoTo error_h
  353.     
  354.     Dim matTemp As D3DMATRIX
  355.     
  356.     'Set the temp matrix to default
  357.     D3DXMatrixIdentity matTemp
  358.     
  359.     'Rotate the matrix on the Y axis
  360.     D3DXMatrixRotationY matTemp, sngAngle
  361.     
  362.     'Combine this transformation with any existing transformations
  363.     D3DXMatrixMultiply matWorld, matWorld, matTemp
  364.     
  365.     Exit Sub
  366. error_h:
  367.     Select Case ErrMsg(Err, "clsUnlitObject.RotateX")
  368.         Case vbRetry
  369.             Resume
  370.         Case vbIgnore
  371.             Resume Next
  372.         Case Else
  373.             Exit Sub
  374.     End Select
  375. End Sub
  376.  
  377. 'This sub stores the texture to be used when drawing this object
  378. Public Sub SetTexture(newTexture As Direct3DTexture8)
  379.     Set Texture = newTexture
  380. End Sub
  381.  
  382. Property Get WorldMatrix() As D3DMATRIX
  383.     WorldMatrix = matWorld
  384. End Property
  385. Property Let WorldMatrix(matNewWorld As D3DMATRIX)
  386.     matWorld = matNewWorld
  387. End Property
  388.  
  389. 'Make sure the object starts at default orientation
  390. Private Sub Class_Initialize()
  391.     D3DXMatrixIdentity matWorld
  392. End Sub
  393.  
  394.  
  395.